﻿<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Puzzles" %>
<%@ Register TagPrefix="validator" TagName="text" Src="~/Controls/Sudoku/ValidatorControl.ascx"%>
<%@ Register TagPrefix="sudoku" TagName="grid" Src="~/Controls/Sudoku/SudokuInput.ascx" %>
<%@ Import Namespace="SudokuState" %>
<%@ Import Namespace="SolverLibrary.Data_Entities" %>
<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        if (Page.IsPostBack)
        {

        }
        validatePuzzle_Click(null, e);
        base.OnLoad(e);
        
    }

    protected void validatePuzzle_Click(object sender, EventArgs e)
    {
        try
        {
            int[][] puzzle = ((SudokuState)Session["puzzle"]).ToArray();
            SudokuValidator.Puzzle valPuzzle = new SudokuValidator.Puzzle(puzzle);
            SudokuValidator.Validator sValidator = new SudokuValidator.Validator(valPuzzle);
            if (sValidator.CheckValidity())
            {
                validator.GetValidatorInput().Text = "Valid";
                validator.GetValidatorInput().BorderColor = System.Drawing.Color.Green;
            }
            else
            {
                validator.GetValidatorInput().Text = "Invalid";
                validator.GetValidatorInput().BorderColor = System.Drawing.Color.Red;


            }
        }
        catch (Exception ex)
        {
            validator.GetValidatorInput().Text = "Invalid";
        }
            

    }

    protected void resetRandom_Click(object sender, EventArgs e)
    {
        ServiceReference1.RandomPuzzleClient client = new ServiceReference1.RandomPuzzleClient();
        int[] array = client.GetRandomPuzzle();
        int[,] twoArray = new int[9,9];
        for (int i = 0; i < array.Length; i++)
        {
            twoArray[i/9,i%9] = array[i];
        }
        Session["puzzle"] = SudokuState.FromMatrix(twoArray);
        Response.Redirect(Request.RawUrl);
        validatePuzzle_Click(sender, e);
    }
    protected void submitPuzzle_Click(object sender, EventArgs e)
    {
        int[][] puzzle = ((SudokuState)Session["puzzle"]).ToArray();
        SudokuBoard board = new SudokuBoard();
        for (int row = 0; row < puzzle.Length; row++)
        {
            board.insertRow(puzzle[row], row);
        }
        System.Diagnostics.Debug.WriteLine(String.Format("Pre-solved board:\n{0}", board));
        SudokuPuzzle sudokuPuzzle = new SudokuPuzzle(board);
        Board solvedBoard = sudokuPuzzle.solve();

        System.Diagnostics.Debug.WriteLine(String.Format("Updating session to solved board:\n{0}", solvedBoard));
        Session["puzzle"] = SudokuState.FromMatrix(solvedBoard.getTwoDimensional());
        Response.Redirect(Request.RawUrl);
        validatePuzzle_Click(sender, e);

    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="Body" runat="Server">
    <sudoku:grid ID="grid" runat="server"></sudoku:grid>
    <validator:text runat="server" ID="validator"/>
    <asp:Button runat="server" Text="Solve!" ID="submitPuzzle" OnClick="submitPuzzle_Click"/>
    <div>
    <asp:Button ID="validatePuzzle" runat="server" Text="Validate!" OnClick="validatePuzzle_Click" />
    </div>

    <div>
    <asp:Button ID="Button1" runat="server" Text="ResetToRandom!" OnClick="resetRandom_Click" />
    </div>
    <script>
        window.onload = document.getElementById("puzzlesNavigator").setAttribute("class", "active");
    </script>
</asp:Content>
